package com.palmap.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangxinju
 * @since 2020/5/7 6:00 下午
 */
@Component
public class MessageProducer implements Runnable {

    private final RedisTemplate<String, Object> redisTemplate;

    public static final String MESSAGE_KEY = "message:queue";
    private volatile AtomicInteger count = new AtomicInteger();

    @Autowired
    public MessageProducer(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @Override
    public void run() {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.leftPush(MESSAGE_KEY, count.incrementAndGet());
    }
}
